﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronSuit.DataMap;
using IronSuit.Crazy250API.LoginAPI;
using IronSuit.Crazy250API.BaseLogic;

namespace IronSuit.Crazy250API.UserAPI
{
	public class UserQueryer
	{
		protected static Dictionary<int, UserHandler> queryerDic = new Dictionary<int, UserHandler>();
		Dictionary<SmallType, Dictionary<string, UserData>> userDataDicDic = new Dictionary<SmallType, Dictionary<string, UserData>>();
		CrazyPeople People;
		protected UserQueryer(CrazyPeople cp)
		{
			People = cp;
		}
		public static UserQueryer GetQueryer(CrazyPeople cp)
		{
			return UserHandler.GetHandler(cp);
		}
		public static UserQueryer GetQueryer(int userID)
		{
			return UserHandler.GetHandler(userID);
		}		
		public UserData QueryData(SmallType category, string key)
		{
			Dictionary<string, UserData> UserDataDic = GetUserDataDic(category);
			if (UserDataDic.ContainsKey(key))
				return UserDataDic[key];
			else
				return null;
		}
		public string QueryDataValue(SmallType category, string key)
		{
			UserData ud = QueryData(category, key);
			if (ud == null)
				return "";
			else
				return ud.Value;
		}
		protected  Dictionary<string, UserData> GetUserDataDic(SmallType category)
		{
			if (!userDataDicDic.ContainsKey(category))
			{
				UserData ud = new UserData();
				ud.Category = category.ToString();
				SetTableName(ud);
				List<UserData> udList = ud.QueryEntityList("UserConn");
				Dictionary<string, UserData> udDic = new Dictionary<string, UserData>();
				foreach (UserData tud in udList)
				{
					udDic[tud.Key] = tud;
				}
				userDataDicDic[category] = udDic;
			}
			Dictionary<string, UserData> UserDataDic = userDataDicDic[category];
			return UserDataDic;
		}

		protected void SetTableName(UserData ud)
		{
			ud.SetTableName("crazy_user" + People.UserDB.ToString() + ".user_" + People.PeopleID.ToString());
		}
	}
}
